<template>
  <section class="todoapp">
    <todo-header @add="addFn"></todo-header>
    <todo-main :list="list" @del="delFn" @change="changFn" @checkAll="checkAllFn"></todo-main>
    <todo-footer
      :list="list"
      @clear="clearFn"
      :type="type"
      @changeType="changeType"
    ></todo-footer>
  </section>
</template>

<script>
import TodoHeader from './components/TodoHeader'
import TodoMain from './components/TodoMain'
import TodoFooter from './components/TodoFooter'

export default {
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
  data() {
    return {
      list: JSON.parse(localStorage.getItem('todoList')) || [],
      type: 'all',
    }
  },
  methods: {
    checkAllFn(value) {
  this.list.forEach((item) => (item.isDone = value))
},
    filter(type) {
      this.type = type
    },
    changeType(type) {
      this.type = type
    },
    delFn(id) {
      // this.$emit('del',id)
      this.list = this.list.filter((item) => item.id !== id)
    },
    changFn(id) {
      const result = this.list.find((item) => item.id === id)
      result.isDone = !result.isDone
    },
    addFn(name) {
      if (!name) {
        alert('请输入内容')
        return
      }
      this.list.unshift({
        id: Date.now(),
        name,
        isDone: false,
      })
    },
    clearFn() {
      this.list = this.list.filter((item) => item.isDone === false)
    },
  },
  watch: {
  list: {
    deep: true,
    handler(newValue) {
      localStorage.setItem('todoList', JSON.stringify(newValue))
    }
  }
},
}
//作业
</script>

<style></style>
